package com.yungu.swift.assets.utils;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.dao.DriverMapper;
import com.yungu.swift.autoconfig.redis.RedisCacheService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.system.sys.model.dto.SysCityDto;
import com.yungu.swift.system.sys.service.SysCityService;
import com.yungu.swift.utils.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Zerrion
 * @version 1.0
 * @description 司机唯一id工具类
 * @date 2018/11/15 11:25
 */
@Component
public class DriverUniqueIdUtils {
    /**
     * 一次生成的编号数量
     */
    public final static int DEFAULT_CREATE_ID_COUNT = 50;


    private static RedisCacheService redisCacheService;
    private static DriverMapper driverMapper;
    private static SysCityService sysCityService;

    private static Lock lock = new ReentrantLock(true);

    @Autowired
    public void setRedisCacheService(RedisCacheService redisCacheService) {
        DriverUniqueIdUtils.redisCacheService = redisCacheService;
    }

    @Autowired(required = false)
    public void setDriverMapper(DriverMapper driverMapper) {
        DriverUniqueIdUtils.driverMapper = driverMapper;
    }

    @Reference
    public void setSysCityService(SysCityService sysCityService) {
        DriverUniqueIdUtils.sysCityService = sysCityService;
    }

    /**
     * 根据城市uuid返回对应的司机编号
     * 获取对应的城市编码 用于拼接司机编号
     *
     * @param cityUuid
     * @return 返回格式如 TJ00001
     */
    public static String getDriverUniqueId(String cityUuid) {
        try {
            lock.lock();
            // 获取当前企业对应的司机编号
            List<String> driverUniqueIds = (List<String>) redisCacheService.get(cityUuid + "driverUniqueIds");
            // 如果司机编号池中为空的话 则需要重新生成一批编号
            if (CollectionUtils.isEmpty(driverUniqueIds)) {
                driverUniqueIds = new ArrayList<>();
                // 获取当前城市的司机数量
                int driverIdCount = driverMapper.count(MapUtils.build("cityUuid", cityUuid));
                // 生成默认数量的司机编号
                for (int i = 0; i < DEFAULT_CREATE_ID_COUNT; i++) {
                    driverUniqueIds.add(String.format("%05d", driverIdCount + i + 1));
                }
            }
            // 取编号池中的第一个编号
            String driverNumId = driverUniqueIds.get(0);
            driverUniqueIds.remove(0);
            // 缓存编号池
            redisCacheService.set(cityUuid + "driverUniqueIds", driverUniqueIds);
            ResponseData<SysCityDto> responseData = sysCityService.get(MapUtils.build("uuid", cityUuid));
            if (responseData.isSuccess() && responseData.getData() != null) {
                SysCityDto sysCityDto = responseData.getData();
                return sysCityDto.getCityNo() + driverNumId;
            }
        } finally {
            lock.unlock();
        }
        return null;
    }

}
